package cn.iocoder.yudao.module.vlog.comment.controller.app;

import cn.iocoder.yudao.framework.business.basic.pojo.vo.UserSimpleVO;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.api.member.user.MemberUserApi;
import cn.iocoder.yudao.module.vlog.comment.bo.VLogCommentBO;
import cn.iocoder.yudao.module.vlog.comment.convert.VLogCommentConvert;
import cn.iocoder.yudao.module.vlog.comment.service.VLogCommentService;
import cn.iocoder.yudao.module.vlog.comment.vo.AppVLogCommentCreateReqVO;
import cn.iocoder.yudao.module.vlog.comment.vo.AppVLogCommentLikeReqVO;
import cn.iocoder.yudao.module.vlog.comment.vo.AppVLogCommentPageReqVO;
import cn.iocoder.yudao.module.vlog.comment.vo.AppVLogCommentRespVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.annotation.security.PermitAll;
import jakarta.validation.Valid;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

@Tag(name = "用户 App - 视频评论")
@RestController
@RequestMapping("/vlog/comment")
@Validated
public class AppVLogCommentController {

    @Resource
    private VLogCommentService commentService;
    @Resource
    private MemberUserApi memberUserApi;

    @PostMapping("/create")
    @Operation(summary = "创建视频评论")
    public CommonResult<Long> createVLogComment(@Valid @RequestBody AppVLogCommentCreateReqVO createReqVO) {
        return success(commentService.createVLogComment(getLoginUserId(), createReqVO));
    }

    @GetMapping("/page")
    @Operation(summary = "获得视频评论分页")
    @PermitAll //无需任何登录
    public CommonResult<PageResult<AppVLogCommentRespVO>> getVLogCommentPage(@Valid AppVLogCommentPageReqVO pageReqVO) {
        PageResult<VLogCommentBO> pageResult = commentService.getVLogCommentPage(pageReqVO);
        Set<Long> userIds = convertSet(pageResult.getList(), VLogCommentBO::getUserId);
        Set<Long> toUerIds = convertSet(pageResult.getList(), VLogCommentBO::getToUserId);
        Set<Long> mergeUserIds = new HashSet<>();
        mergeUserIds.addAll(userIds);
        mergeUserIds.addAll(toUerIds);
        List<UserSimpleVO> users = memberUserApi.getUserSimpleList(mergeUserIds);
        return success(VLogCommentConvert.INSTANCE.convertAppPage(VLogCommentConvert.INSTANCE.convertPage(pageResult, users)));
    }

    @GetMapping("/list-by-parent-ids")
    @Operation(summary = "批量获得视频评论列表")
    @Parameter(name = "parentIds", description = "父评论编号列表", required = true, example = "1,2,3")
    @PermitAll //无需任何登录
    public CommonResult<List<AppVLogCommentRespVO>> getVLogCommentListByParentId(@RequestParam("parentIds") List<Long> parentIds) {
        List<VLogCommentBO> list = commentService.getVLogCommentListByParentId(parentIds);
        Set<Long> userIds = convertSet(list, VLogCommentBO::getUserId);
        Set<Long> toUerIds = convertSet(list, VLogCommentBO::getToUserId);
        Set<Long> mergeUserIds = new HashSet<>();
        mergeUserIds.addAll(userIds);
        mergeUserIds.addAll(toUerIds);
        List<UserSimpleVO> users = memberUserApi.getUserSimpleList(mergeUserIds);
        return success(VLogCommentConvert.INSTANCE.convertList(list, users));
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除视频评论评论")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteVLogComment(@RequestParam("id") Long id) {
        commentService.deleteVLogComment(getLoginUserId(), id);
        return success(true);
    }

    // ==================== 子表（视频评论点赞） ====================

    @PostMapping("/like")
    @Operation(summary = "视频评论点赞")
    public CommonResult<Boolean> like(@Valid @RequestBody AppVLogCommentLikeReqVO reqVO) {
        commentService.likeVLogComment(getLoginUserId(), reqVO.getCommentId());
        return success(true);
    }

    @PostMapping("unlike")
    @Operation(summary = "取消视频评论点赞")
    public CommonResult<Boolean> unlike(@Valid @RequestBody AppVLogCommentLikeReqVO reqVO) {
        commentService.unlikeVLogComment(getLoginUserId(), reqVO.getCommentId());
        return success(true);
    }

    @GetMapping("/liked/list-user-by-comment-id")
    @Operation(summary = "获得视频评论的点赞用户列表")
    @Parameter(name = "commentId", description = "视频评论编号", required = true)
    @PermitAll //无需任何登录
    public CommonResult<List<UserSimpleVO>> getVLogCommentLikedUserListByCommentId(@RequestParam("commentId") Long commentId) {
        Set<Long> userIds = commentService.getVLogCommentLikedUserIds(commentId);
        return success(memberUserApi.getUserSimpleList(userIds));
    }

}
